<template>
  <div>
    <x-header :left-options="{backText: ''}" @on-click-more="showMenus = true" >发消息</x-header>
    <div class="ui-body" id="uploadImg">
      <div>
        <div style="min-height: 300px;position: relative;overflow: hidden;background-color: white;">
          <span v-if="!imgPath" style="color: #999;position: absolute;top:50%;left: 50%;-webkit-transform: translate(-50%, -50%);transform: translate(-50%, -50%);">点击添加图</span>
          <input type="file" accept="image/*" ref="titleImg" style="position: absolute;width:100%;height: 100%;top:0;opacity: 0;z-index: 999;"> 
          <img v-if="imgPath" :src="imgPath" style="width: 100%;display: block;position: absolute;top: 50%;-webkit-transform: translateY(-50%);transform: translateY(-50%);"></img>
          <div style="z-index:999;white-space:nowrap;overflow: hidden;text-overflow: ellipsis;background-color:rgba(0,0,0,0.6);color: white;text-align: center;line-height: 40px;font-size: 18px;position: absolute;bottom: 0;width: 100%;" @click="$router.go(-1)">{{$store.state.title}}</div>
        </div>
        <div  style="background-color: white;margin-top: 20px;">
          <x-textarea v-model="desc" placeholder="请填写投票标题" :height="200" @on-blur="saveText"></x-textarea>
        </div>
        <group>
          <cell title="发送范围" value="" is-link link="messageto"></cell>
        </group>
        
        <box gap="10px 10px">
          <x-button  type="primary" @click.native="messageSend" style="margin-top: 20px;">推送消息</x-button>
        </box>
      </div>
    </div>
  </div>
</template>

<script>
import { Group, Cell, XTextarea, XImg, XButton, Box, XHeader, Flexbox, FlexboxItem, Actionsheet } from 'vux'
import store from '@/store'
import axios from 'axios'
require('es6-promise').polyfill()
import weui from '../../static/weui.min.js'
var qs = require('qs')

export default {
  store,
  components: {
    Group,
    Cell,
    XTextarea,
    XImg,
    XButton,
    Box,
    Flexbox,
    FlexboxItem,
    XHeader,
    Actionsheet
  },
  data () {
    return {
      imgPath: '',
      desc: ''
    }
  },
  mounted () {
    this.initUpload()
    if (this.$route.params.id) {
      this.$store.commit('updateState', {key: 'message_VoteId', value: this.$route.params.id})
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.imgPath = vm.$store.state.message_img
      vm.desc = vm.$store.state.message_desc
    })
  },
  methods: {
    initUpload () {
      let _this = this
      weui.uploader('#uploadImg', {
        url: _this.$store.state.url + '/VoteManage/VoteImageUpload.ashx',
        auto: true,
        type: 'base64',
        fileVal: 'uploadImg',
        compress: {
          quality: 0.5
        },
        onBeforeQueued: function (files) {
          _this.$vux.loading.show({
            text: '上传中...'
          })
          if (['image/jpg', 'image/jpeg', 'image/png', 'image/gif'].indexOf(this.type) < 0) {
            _this.$vux.toast.text('请上传图片', 'bottom')
            _this.$vux.loading.hide()
            return false
          }
        },
        onQueued: function () {
          _this.upLoadImg(this.base64)
        },
        onBeforeSend: function (data, headers) {
          return false // 阻止文件上传
        }
      })
    },
    upLoadImg (Base64) {
      let _this = this
      axios.post(this.$store.state.url + '/VoteManage/VoteImageUpload.ashx', qs.stringify({Base64: Base64}))
      .then((rs) => {
        let data = rs.data
        if (data.code === 200) {
          _this.imgPath = _this.$store.state.imgUrl + data.url
          this.$store.commit('updateState', {key: 'message_img', value: _this.imgPath})
          _this.$vux.loading.hide()
        } else {
          this.$vux.toast.text(data.msg, 'bottom')
        }
        _this.$vux.loading.hide()
      })
      .catch((response) => {
        this.$vux.toast.text('请求失败请稍后再试', 'bottom')
      })
    },
    saveText () {
      this.$store.commit('updateState', {key: 'message_desc', value: this.desc})
    },
    messageSend () {
      if (!this.$store.state.message_VoteId) {
        this.$vux.toast.text('投票ID获取失败', 'bottom')
        this.$router.go(-1)
        return
      }
      if (!this.imgPath) {
        this.$vux.toast.text('请上传图片', 'bottom')
        return
      }
      if (!this.desc) {
        this.$vux.toast.text('请输入投票描述', 'bottom')
        return
      }
      if (!this.$store.state.SelectedEmployeeIds.length) {
        this.$vux.toast.text('请选择发送的范围', 'bottom')
        return
      }
      let json = {
        ImgUrl: this.imgPath,
        Title: this.desc,
        EmployeeId: document.getElementById('Creater').value || 'F76D052A-569A-48B9-A8E3-7E6D3CCEDFA4',
        VoteId: this.$store.state.message_VoteId,
        EnterpriseId: document.getElementById('EnterpriseID').value
      }
      let addAllSelectedChild = (item) => {
        let SelectedEmployeeIds = []
        let SelectedOrgIds = []
        if (this.$store.state.SelectedEmployeeIds.indexOf(item.OrgID) !== -1) {
          SelectedOrgIds.push(item.OrgID)
        } else {
          for (let index in item.ChildListOrgMail) {
            let [EIds, OIds] = addAllSelectedChild(item.ChildListOrgMail[index])
            SelectedEmployeeIds = [...SelectedEmployeeIds, ...EIds]
            SelectedOrgIds = [...SelectedOrgIds, ...OIds]
          }
          for (let index in item.ListMail) {
            if (this.$store.state.SelectedEmployeeIds.indexOf(item.ListMail[index].key) > -1) {
              SelectedEmployeeIds.push(item.ListMail[index].key)
            }
          }
        }
        console.log(SelectedEmployeeIds)
        console.log(SelectedOrgIds)
        return [SelectedEmployeeIds, SelectedOrgIds]
      }
      let [SelectedEmployeeIds, SelectedOrgIds] = addAllSelectedChild(this.$store.state.commonList[0])
      json['SelectedEmployeeIds'] = SelectedEmployeeIds.join('|')
      json['SelectedOrgIds'] = SelectedOrgIds.join('|')
      this.$vux.loading.show({
        text: '推送中...'
      })
      axios.post(this.$store.state.url + '/VoteManage/SendMessage.ashx', qs.stringify(json))
      .then((rs) => {
        let data = rs.data
        if (data.status === 200) {
          this.$vux.toast.text('推送成功', 'bottom')
          this.$router.go(-1)
        } else {
          this.$vux.toast.text(data.msg, 'bottom')
        }
        this.$vux.loading.hide()
      })
      .catch((response) => {
        this.$vux.loading.hide()
        this.$vux.toast.text('请求失败请稍后再试', 'bottom')
      })
    }
  }
}
</script>

<style scoped src='@/assets/css/edit-two.css'></style>
